/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db;

import models.Person;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import common.Md5;

/**
 *
 * @author pelle
 */
public class LoginDb extends AbstractDb implements LoginInterface {

    private PersonInterface personDb = new PersonDb();
    
    public Person getLoginPerson(String email, String password) {
        Person person = null;
        try {
            String md5Password = Md5.md5(password);
            ResultSet result = this.db.query("SELECT TOP 1 person_id, person_password FROM Lif_Person WHERE person_email = '" + email + "'");
            
            if(result != null && result.next()) {
                // MD5 password matchup!
                if(md5Password.equals(result.getString("person_password"))) {
                    int userId = result.getInt("person_id");
                    person = personDb.getPersonById(userId);
                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(LoginDb.class.getName()).log(Level.SEVERE, null, ex);
        }

        return person;
    }
    
}